package com.finace.money.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.QryData;
import com.finace.money.ui.fragment.MainStatusFragment;
import com.finace.money.ui.widget.CustomHScrollView;

import java.util.List;

/**
 * Created by zhangpeng on 2017/11/27.
 */

public class HaveListViewAdapter extends BaseAdapter {

    private List<QryData> mData;
    private LayoutInflater mInflater;
    private RelativeLayout mHead;
    private int n = 1;//标记变量

    public void setmData(List<QryData> mData) {
        this.mData = mData;
    }

    public HaveListViewAdapter(RelativeLayout head) {
        this.mHead = head;
        this.mInflater = LayoutInflater.from(UIUtil.getContext());
    }

    @Override
    public int getCount() {
        return (null != mData && mData.size() > 0) ? mData.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup group) {
        MyViewHolder holder = null;
        if (view == null) {
            view = mInflater.inflate(R.layout.item_have_list, group, false);
            holder = new MyViewHolder();
            CustomHScrollView scrollView = view.findViewById(R.id.h_scrollView);
            holder.scrollView = scrollView;
            holder.mTvDateTip = view.findViewById(R.id.tv_date_tip);
            holder.mTextView1 = view.findViewById(R.id.textView_1);
            holder.mTextView2 = view.findViewById(R.id.textView_2);
            holder.mTextView3 = view.findViewById(R.id.textView_3);
            holder.mTextView4 = view.findViewById(R.id.textView_4);
            holder.mTextView5 = view.findViewById(R.id.textView_5);
            holder.mTextView6 = view.findViewById(R.id.textView_6);
            holder.mTextView7 = view.findViewById(R.id.textView_7);
            holder.mTextView8 = view.findViewById(R.id.textView_8);
            holder.mTextView9 = view.findViewById(R.id.textView_9);

            CustomHScrollView headSrcrollView = (CustomHScrollView) mHead.findViewById(R.id.h_scrollView);
            headSrcrollView.AddOnScrollChangedListener(new OnScrollChangedListenerImp(scrollView));

            view.setTag(holder);
        } else {
            holder = (MyViewHolder) view.getTag();
        }
        String text0 = mData.get(i).getText0();
        holder.mTvDateTip.setText(text0);
        holder.mTextView1.setText(mData.get(i).getText1());
        holder.mTextView2.setText(mData.get(i).getText2());
        holder.mTextView3.setText(mData.get(i).getText3());
        holder.mTextView4.setText(mData.get(i).getText4());
        holder.mTextView5.setText(mData.get(i).getText5());
        holder.mTextView6.setText(mData.get(i).getText6());
        holder.mTextView7.setText(mData.get(i).getText7());
        holder.mTextView8.setText(mData.get(i).getText8());
        holder.mTextView9.setText(mData.get(i).getText9());
        if (StringUtil.isNotNullString(text0)) {
            holder.mTvDateTip.setVisibility(View.VISIBLE);
            if (text0.equals(UIUtil.getString(R.string.have_today))) {
                holder.mTvDateTip.setBackgroundColor(UIUtil.getColor(R.color.colorYellowDDC09C));
            } else if (text0.equals(UIUtil.getString(R.string.have_yestoday))) {
                holder.mTvDateTip.setBackgroundColor(UIUtil.getColor(R.color.colorWhite899198));
            } else {
                holder.mTvDateTip.setBackgroundColor(UIUtil.getColor(R.color.colorYellowDDC09C));
            }
        } else {
            holder.mTvDateTip.setVisibility(View.GONE);
        }

        return view;
    }

    class OnScrollChangedListenerImp implements CustomHScrollView.OnScrollChangedListener {
        CustomHScrollView mScrollViewArg;

        public OnScrollChangedListenerImp(CustomHScrollView scrollViewar) {
            mScrollViewArg = scrollViewar;
        }

        @Override
        public void onScrollChanged(int l, int t, int oldl, int oldt) {
            mScrollViewArg.smoothScrollTo(l, t);
            if (n == 1) {//记录滚动的起始位置，避免因刷新数据引起错乱
                new MainStatusFragment().setPosData(oldl, oldt);
            }
            n++;
        }
    }

    class MyViewHolder {
        TextView mTvDateTip;
        TextView mTextView1;
        TextView mTextView2;
        TextView mTextView3;
        TextView mTextView4;
        TextView mTextView5;
        TextView mTextView6;
        TextView mTextView7;
        TextView mTextView8;
        TextView mTextView9;
        HorizontalScrollView scrollView;
    }
}
